package com.fly.controller;

import com.fly.bean.Defects;
import com.fly.bean.SystemPropertiesValue;
import com.fly.service.DefectsServiceO;
import com.fly.util.Msg;
import com.fly.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther20: Orchid
 * @Date: 19/11/30 17:23
 * @Description:
 */
@Controller
@RequestMapping("/defectsOrchid")
public class DefectsControllerO {

    @Autowired
    DefectsServiceO defectsServiceO;

    /**
     * 分页查询缺陷信息
     *
     * @param page 分页信息实体
     * @return
     */
    @RequestMapping(value = "/getDefectsByPage")
    @ResponseBody
    public Msg getDefectsByPage(@RequestBody Page page) {

        //分页查询缺陷信息
        List<Defects> defects = defectsServiceO.queryDefectsByPage(page);

        //查询缺陷总数
        Integer count = defectsServiceO.queryAllCount();

        Msg msg = Msg.success();

        msg.add("defects", defects);
        msg.add("count", count);

        return msg;
    }

    /**
     * 修改缺陷是否启用状态
     *
     * @return 成功结果数
     */
    @RequestMapping("/updateDefectsState")
    @ResponseBody
    public Msg updateDefectsState(String defectsState, Integer defectsId, String defectsName) {

        if ("停用".equals(defectsState)) {

            Boolean isUseInDamage = defectsServiceO.queryIsUseInDamage(defectsId);

            if (isUseInDamage) {
                return Msg.fail().add("error", "此缺陷信息正在使用，无法停用。");
            }

        }

        //设置缺陷查询信息
        Defects defects = new Defects();
        defects.setDefectsId(defectsId);
        defects.setDefectsName(defectsName);

        SystemPropertiesValue systemPropertiesValue = new SystemPropertiesValue();
        systemPropertiesValue.setSysProValueName(defectsState);
        defects.setSystemPropertiesValue(systemPropertiesValue);

        //修改状态
        Boolean isUp = defectsServiceO.updateDefectsState(defects);

        if (isUp) {
            return Msg.success();
        } else {
            return Msg.fail().add("error", "修改失败");
        }
    }

    /**
     * 根据id查询缺陷信息
     *
     * @param defectsId 缺陷id
     * @return
     */
    @RequestMapping("/getDefectsById")
    @ResponseBody
    public Msg getDefectsById(Integer defectsId) {

        Defects defects = defectsServiceO.queryDefectsById(defectsId);

        if (defects != null) {
            return Msg.success().add("defects", defects);
        } else {
            return Msg.fail().add("error", "查询失败，或id不存在");
        }

    }

    /**
     * 根据id删除缺陷信息
     *
     * @param defectsId 缺陷信息id
     * @return
     */
    @RequestMapping("/deleteDefectsById")
    @ResponseBody
    public Msg deleteDefectsById(Integer defectsId) {

        //判断此状态是否正在使用
        Boolean isUseInDamage = defectsServiceO.queryIsUseInDamage(defectsId);

        if (isUseInDamage) {
            return Msg.fail().add("error", "此缺陷信息正在使用，无法停用。");
        }

        //判断缺陷信息是否是启用状态
        Boolean isUse = defectsServiceO.queryDefectsIsUse(defectsId);

        if (isUse) {
            return Msg.fail().add("error", "该缺陷信息处于启用状态，无法删除。");
        }

        Boolean isDel = defectsServiceO.deleteDefectsById(defectsId);

        if (isDel) {
            return Msg.success();
        } else {
            return Msg.fail().add("error", "删除失败，或id不存在");
        }

    }

    /**
     * 添加缺陷信息
     *
     * @param defectsName  缺陷名称
     * @param defectsState 缺陷状态是否启用
     * @return
     */
    @RequestMapping("/addDefects")
    @ResponseBody
    public Msg addDefects(String defectsName, String defectsState) {

        //设置缺陷添加信息
        Defects defects = new Defects();
        defects.setDefectsName(defectsName);

        SystemPropertiesValue systemPropertiesValue = new SystemPropertiesValue();
        systemPropertiesValue.setSysProValueName(defectsState);

        defects.setSystemPropertiesValue(systemPropertiesValue);


        Boolean isExit = defectsServiceO.queryDefectsByName(defectsName);

        if (isExit) {
            return Msg.fail().add("error", "该缺陷名称已存在");
        }

        //添加缺陷信息
        Boolean isAdd = defectsServiceO.addDefects(defects);

        if (isAdd) {
            return Msg.success();
        } else {
            return Msg.fail().add("error", "添加失败");
        }

    }

}
